
package com.theme.common.office.dom4j;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * 此类描述的是: xml工具助手
 * @author: cuixinfu@ralncy.com
 * @date:2017年11月21日 下午1:28:59
 */
public class Dom4jHandler {

	public void creatXml(File file) throws IOException {
		//创建一个文档模型
		Document document  = DocumentHelper.createDocument();

		document.addDocType("plist","-//Apple//DTD PLIST 1.0//EN","http://www.apple.com/DTDs/PropertyList-1.0.dtd");
		//定义一个根节点
		Element root = document.addElement("plist");
		root.addAttribute("version", "1.0");

		//定义一个根节点
		Element dict = root.addElement("dict");
		//根节点下添加子节点
		Element paramer1 = dict.addElement("key");
		//节点添加文本
		paramer1.addText("PayloadContent");
		Element array = dict.addElement("array");

		// ======================
		Element dict2 = array.addElement("dict");
		Element guodaxia1 = dict2.addElement("key");
		guodaxia1.addText("FullScreen");
		Element guodaxiaFalse = dict2.addElement("false");
		Element guodaxiaIcon = dict2.addElement("key");
		guodaxiaIcon.addText("Icon");
		Element guodaxiaData = dict2.addElement("data");
		//可重复的元素
		Element guodaxia2 = dict2.addElement("key");
		guodaxia2.addText("IsRemovable");
		Element guodaxia21 = dict2.addElement("true");
		Element guodaxia3 = dict2.addElement("key");
		Element guodaxia31 = dict2.addElement("string");
		guodaxia3.addText("Label");
		guodaxia31.addText("微博");

		Element guodaxia4 = dict2.addElement("key");
		Element guodaxia41 = dict2.addElement("string");
		guodaxia4.addText("PayloadDescription");
		guodaxia41.addText("配置 Web Clip 设置");

		// ===========重复元素===========
		Element paramer4 = dict.addElement("key");
		paramer4.addText("keys");
		Element paramer5 = dict.addElement("string");
		paramer5.addText("values");

		Element paramer6 = dict.addElement("key");
		paramer6.addText("keys");
		Element paramer61 = dict.addElement("string");
		paramer61.addText("values");

		Element paramer7 = dict.addElement("key");
		paramer7.addText("keys");
		Element paramer71 = dict.addElement("string");
		paramer71.addText("values");

		Element paramer8 = dict.addElement("key");
		paramer8.addText("keys");
		Element paramer81 = dict.addElement("string");
		paramer81.addText("values");

		Element paramer11 = dict.addElement("key");
		Element paramer12 = dict.addElement("string");
		paramer11.addText("PayloadVersion");
		paramer12.addText("1");

		//创建XML格式
		OutputFormat xmlFormat = new OutputFormat();
		xmlFormat.setEncoding("UTF-8");
		xmlFormat.setNewlines(true);
		xmlFormat.setIndent("      ");

		FileWriter fileWriter = new FileWriter(file);
		XMLWriter xmlWriter = new XMLWriter(fileWriter,xmlFormat);
		xmlWriter.write(document);
		xmlWriter.close();
	}

	//获取节点名，节点属性
	public void iteratorCherk(Element element) {
		List<Element> elements = element.elements();
		for(Element ele : elements){
			List<Attribute> attributeList = ele.attributes();
			String name = ele.getName();
			String text = ele.getTextTrim();
			System.out.println(name + " " + text + "  " + "节点");
			for(Attribute att : attributeList){
				String aname = att.getName();
				String atext = att.getText();
				System.out.println(aname + " " + atext + "  " + "属性");
			}
			System.out.println("----------------------------------------");
			iteratorCherk(ele);
		}
	}

	//获取节点名，节点属性
	boolean flag = false;
	int out = 0;
	String label = "0";
	public void iteratorCherk2(Element element) {
		List<Element> elements = element.elements();
		for(Element ele : elements){
			List<Attribute> attributeList = ele.attributes();
			String name = ele.getName();
			String text = ele.getTextTrim();
			if (text.equals("Label") || text.equals("URL") || text.equals("TargetApplicationBundleIdentifier")){
				out = out + 1 ;
				label = text;
				//System.out.println(text);
				for(Attribute att : attributeList){
					String aname = att.getName();
					String atext = att.getText();
					System.out.println(aname + " " + atext + "  " + "属性");
				}
				flag = true ;
			}
			if (name.equals("string") && flag){
				flag = false;
				out = out + 1 ;
				System.out.println(label+":" + text);
				for(Attribute att : attributeList){
					String aname = att.getName();
					String atext = att.getText();
					System.out.println(aname + " " + atext + "  " + "属性");
				}
				if (out == 6){
					out = 0;
					System.out.println("---------------------end--------------------");
				}
			}
			iteratorCherk2(ele);
		}
	}

	//解析XML
	public void analysisXml(File file) throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(file);
		Element root = document.getRootElement();
		iteratorCherk(root);

	}

	public static void main(String[] args) throws Exception {
		Dom4jHandler taoDaShen = new Dom4jHandler();
		String filePath = "E:\\测试文件\\xml\\test.xml" ;
		File file = new File(filePath);
		//生成xml文件
		taoDaShen.creatXml(file);

		//解析xml文件
		taoDaShen.analysisXml(file);

		//上传文件到oss
		String suffix = filePath.substring(filePath.lastIndexOf("."));

	}

}
